package {
	import flash.events.Event;
	import com.molamil.outboard.MidiService;
	import com.molamil.outboard.Outboard;

	import flash.geom.Point;
	import flash.display.Sprite;
	import flash.ui.Keyboard;

	/**
	 * @author martindyrby
	 */
	public class OutboardMain extends Sprite {
		private var outb:Outboard;
		private var midiservice : MidiService;
		
		private var onKeyDownIndex : int;
		private var onKeyUpIndex : int;
		private var onMouseMoveIndex : int;
		private var onEnterIndex : int;
		private var drawY : Number = 0;
		private var drawX : Number = 0;

		public function OutboardMain() {
			outb = new Outboard(stage);
			outb.startRecording(setupDrawing, new Point(mouseX, mouseY));
			
			onKeyDownIndex = outb.onKeyDown(Keyboard.UP, onKeyDown);
			onKeyUpIndex = outb.onKeyUp(Keyboard.UP, onKeyUp);
			
			outb.onKeyUp(Keyboard.ESCAPE, onEscape);

			//onMouseMoveIndex = outb.onMouseMove(onMouseMove);
			outb.onMouseDown(onMouseDown);
			outb.onMouseUp(onMouseUp);
			
			midiservice = new MidiService();
			midiservice.addEventListener(Event.CONNECT, onMidiServerConnect, false, 0, true);
			midiservice.init("localhost", 1313);			
			
			outb.registerServer(midiservice);
			
			
			var p:Sprite = new Sprite();
			p.graphics.beginFill(0);
			p.graphics.drawRect(0, 0, 100, 100);
			p.graphics.endFill();
			
			addChild(p);
			
			outb.adjustPositionWithKeyboard("black", p, 1);
		}

		private function onMidiServerConnect(event : Event) : void {
			midiservice.onMidiControl(1, onKnobOne);
			midiservice.onMidiControl(2, onKnobTwo);
			midiservice.onMidiNoteOn(1, onKnobOne);
			midiservice.onMidiNoteOn(2, onKnobTwo);
		}

		private function onKnobTwo(value:uint) : void {
			drawY = (value/127) * stage.stageHeight;
			graphics.lineTo(drawX, drawY);
			trace("draw", drawX, drawY);			
		}

		private function onKnobOne(value:uint) : void {
			drawX = (value/127) * stage.stageWidth;
			graphics.lineTo(drawX, drawY);
			trace("draw", drawX, drawY);
		}

		private function setupDrawing(p:Point) : void {
			graphics.lineStyle(1, 0xff0000);
			graphics.moveTo(0, 0);
		}

		private function onMouseUp(p:Point) : void {
		}

		private function onMouseDown(p:Point, data:Object = null) : void {
			if(data != null) {
				graphics.lineStyle(data.linethickness, data.color);
			} else {
				var obj:Object = {linethickness: Math.random()*9+1, color: Math.random()*0xeeeeee};
				graphics.lineStyle(obj.linethickness, obj.color);
				outb.addDataToLastRecordingEntry(obj);
			}
		}

		private function onMouseMove(p:Point) : void {
			graphics.lineTo(p.x, p.y);
		}

		private function onEnter() : void {
			outb.deactivateListener(onEnterIndex);
			outb.playback();
		}

		private function onEscape() : void {
			outb.endRecording();
			graphics.clear();
			graphics.lineStyle(1, 0xff0000);
			outb.deactivateListener(onMouseMoveIndex);
			onEnterIndex = outb.onKeyUp(Keyboard.ENTER, onEnter);			
		}

		private function onKeyDown() : void {
			trace("UP pressed down");
		}

		private function onKeyUp() : void {
			trace("UP pressed up");
		}
	}
}
